<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>
<script>
import echarts from 'echarts'
import resize from './mixins/resize'

require('echarts/theme/macarons') // echarts theme

export default {
  name: 'RadarChart',
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null

    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      if (this.chartData != null) {
        // this.chartData.name = ['照明系统', '通风控制器系统', '排风系统', '新风系统', '空调系统']
        // this.chartData.date = ['4/6', '4/7']
        // this.chartData.content = [[1, 2], [1, 2], [1, 2], [1, 2], [1, 2]]
        this.setOptions(this.chartData)
      }
    },
    initSeries(name, content, date) {
      const array = []
      if (content.length === 0 || content[0] === null) {
        return array
      }
      for (let i = 0; i < name.length; i++) {
        array.push({
          name: name[i],
          type: 'bar',
          stack: 'devices',
          emphasis: {
            focus: 'series'
          },
          data: content[i]
        })
      }
      return array
    },
    setOptions({name, content, date} = {}) {
      this.chart.setOption({
        title: {
          text: '能耗对比',
          subtext: '按系统',
          top: 10,
          left: 10
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: { // 坐标轴指示器，坐标轴触发有效
            type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
          }
        },
        legend: {
          data: name,
          type: 'scroll',
          bottom: '1%'
        },
        grid: {
          left: '1%',
          right: '1%',
          bottom: '8%',
          containLabel: true
        },
        xAxis: [
          {
            type: 'category',
            data: date
          }
        ],
        yAxis: [
          {
            type: 'value',
            position: 'right'
          }
        ],
        series: this.initSeries(name, content, date)
      })
    }
  }
}
</script>

<style scoped>

</style>
